import java.io.IOException;

import javax.xml.stream.XMLStreamException;

public class RobotInterface {
	private XMLHandler outXMLHandler;
	private XMLHandler inXMLHandler;
	private BluetoothConnection btc;
	private String robotName;

	public RobotInterface(String name) {
		this.robotName = name;
		this.btc = new BluetoothConnection();
		this.outXMLHandler = new XMLHandler();
		this.inXMLHandler = new XMLHandler();
		this.btc.waitforbtconnection(this.robotName);
	}

	public void closeConnection() {
		this.btc.closeConnection();
	}

	public boolean isConnected() {
		return this.btc.isConnected(); // Must fix in BluetoothConnection.java
	}

	public XMLHandler getOutXMLHandler() {
		return this.outXMLHandler;
	}

	public XMLHandler getInXMLHandler() {
		return this.inXMLHandler;
	}
	
	public void clear() {
		this.outXMLHandler = new XMLHandler();
		this.inXMLHandler = new XMLHandler();
	}
	
	public void clearOutput() {
		this.outXMLHandler = new XMLHandler();
	}
	
	public void clearInput() {
		this.inXMLHandler = new XMLHandler();
	}

	public void send() throws IOException {
		this.btc.send(this.outXMLHandler.toString("s2r"));
	}

	public void send(String s) throws IOException {
		this.btc.send(s);
	}

	public void recieve() throws XMLStreamException, IOException {
		this.inXMLHandler.fromXML(btc.recieveData());
	}

	public void addMoveDistance(int cm) {
		this.outXMLHandler.addCommand(new ServerCommand("move",
				new CommandParameter("req", cm)));
	}

	// Should we let people rotate any degrees they want, or can we only move in
	// 4 directions?
	public void addRotate(int degrees) {
		this.outXMLHandler.addCommand(new ServerCommand("rotate",
				new CommandParameter("req", degrees)));
	}

	public void addScan(int numOfScans) {
		this.outXMLHandler.addCommand(new ServerCommand("scan",
				new CommandParameter("req", numOfScans)));
	}

	public void addReboot() {
		this.outXMLHandler.addCommand(new ServerCommand("reboot"));
	}

	// function for some special commands from our group

	public void addMoveForward() // move forward indefinitely,
	{
		this.outXMLHandler.addCommand(new ServerCommand("G2moveForward"));
	}

	public void addMoveBackward() // move backward indefinitely,
	{
		this.outXMLHandler.addCommand(new ServerCommand("G2moveBackward"));
	}

	public void addStop() {
		this.outXMLHandler.addCommand(new ServerCommand("G2stop"));
	}

	public void addAutoMode() // switch robot to automode
	{
		this.outXMLHandler.addCommand(new ServerCommand("G2autoMode"));
	}

	public void addClose() {
		this.outXMLHandler.addCommand(new ServerCommand("G2close"));
	}

	/**
	 * Moves the robot backwards through the commands. Used to return to starting point.
	 * @throws IOException 
	 */
	public void returnToStart() throws IOException {
		
		XMLHandler newXML = new XMLHandler();
		
		for (int i = outXMLHandler.getCommand().size() - 1; i >= 0; i--) {
			if (outXMLHandler.isMove(i)) {
				newXML.addCommand(new ServerCommand("move", new CommandParameter("req", outXMLHandler.getValue(i, "req") * -1)));
			} else if (outXMLHandler.isRotate(i)) {
				newXML.addCommand(new ServerCommand("rotate", new CommandParameter("req", outXMLHandler.getValue(i, "req") * -1)));
			}
		}	
		
		this.btc.send(newXML.toString("s2r"));
		
	}

}